// 云函数入口文件
/*在点击“+”或者点击加入购物车判断购物车中有这个商品时调用*/
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
const _  = db.command
// 云函数入口函数
exports.main = async (event, context) => {
  // var pids = event.pids
  var uid = event.uid
  var id = event.id
  var num = event.num
  var data = {id, num}
  return await db.collection("cart").where({
    uid:uid,
    goods:{
      id:id
    }
   })
  .update({
    data:{
      // pids:_.push([pids])
      /*
      这种添加方式必须指明下标
      */
      // ['goods.'+0+'.id']:"00001",
      // ['goods.'+0+'.num']:10
      //goods:_.push(data)
      pids:_.pull({
        id:_.eq(id)
      }),
      goods:_.push(data)
      //num:_.inc(1)这样在数组外面加了一个num

    }
  })

}